/** @file dialog.cpp
 */
#include "dialog.h"
#include "FreeRTOS.h"
#include "task.h"


extern Dialog *ancestor;
/** @brief  create a dialog, with parent =hWnd
 */
Dialog::Dialog(HWND parent):Window(parent)
{
    activable=1;
}

Dialog::~Dialog()
{
}

void Dialog::draw(Rect *r)
{
    pDC->fillRect(rect.x0, rect.y0, rect.x1, rect.y1, 0x00);

    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);
}

int Dialog::run()
{
    Msg msg;
    
    create();
    while( 1)
    {
        if(lifeState ==0)
        {
            return lifeState;
        }
        if(lifeState == -1)
        {
            return lifeState;
        }
        
        if(::getMessage(&msg))
        {
            if( (msg.iMsg ==MSG_KEY_UP) || (msg.iMsg ==MSG_KEY_DOWN) ) msg.hWnd =this;

            sendMessage(msg.hWnd, msg.iMsg, msg.wParam, msg.lParam);
        }
        else
        {
        }
    }
}

/** @brief dialog event handler
 */
int Dialog::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{

    switch(iMsg)
    {
    case MSG_KEY_DOWN:
        //Child have the Active status
        if(activeChild)
        {
            sendMessage( activeChild, iMsg, wParam, lParam);
        }
        
        //need key focus
        else
        {
            Widget::eventHandler(hWnd, iMsg, wParam, lParam);
        }
        break;
        
    case MSG_KEY_UP:
        if(activeChild)
        {
            sendMessage( activeChild, iMsg, wParam, lParam);
        }
        break;

        //father's duty
    case MSG_ACTIVE_ELDER_CHILD:
        activePrevChild();
        break;
        
    case MSG_ACTIVE_YOUNGER_CHILD:
        activeNextChild();
        break;
    }

}

